<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;


class UserController extends Controller
{

	//后台用户管理首页方法  GET
    public function index()
    {
        //include "../app/Common/function.php";
     //  speak1();//自定义函数
      //  exit();
        // 连贯操作

        //查询所有操作
        //$data = DB::select("select * from user");
        //$data = DB::table("user")->get();
        //查询一条数据
      //  $data =DB::table("user")->find("2");
        //$data = DB::table("user")->where("id",2)->first();
       // $data = DB::table("user")->where("id","<",5)->get();
       // $data = DB::table("user")->where([['id','>=',2],['id','<=',29]])->get();
       // $data = DB::table('user')->where([['name','like','%2%'],['id','>=',10]])->get();
        //$data = DB::table('user')->where("name","like","%2%")->orwhere("pass","123")->get();
      //  $data= DB::table("user")->whereBetween('id',[2,28])->get();
       // $data= DB::table("user")->whereNotBetween('id',[2,28])->get();
       // $data=DB::table("user")->whereIn("id",[2,27,30])->get();
        //$data=DB::table("user")->whereNotIn("id",[2,27,30])->get();
       // $data=DB::table("pic")->whereNull("img")->get();
       // $data=DB::table("pic")->whereNotNull("img")->get();
       // $data=DB::table("user")->where("id",2)->value("name");
     //   $data=DB::table("user")->pluck("name");//获取一列的值
       // echo DB::table("user")->count();
      //  echo DB::table("user")->max('time');
      //  echo "<hr>";
      //  echo DB::table("user")->min('time');
      //  echo "<hr>";
       // echo DB::table("user")->avg('time');
      //  echo "<hr>";
      //  echo DB::table("user")->sum('name');

        //select count(*) tot from user
       // $data = DB::table("user")->select(DB::raw("count(*) tot"))->first();
        //dd($data);
       //exit;
       // var_dump($data[0]);exit;
        $data = DB::table("user")->orderBy("id","desc")->paginate('5');//simplePaginate
            //dd($data);
		//加载用户管理界面
			return view('admin.user.index')->with("data",$data);
    }
	
	//后台用户管理修改页面 GET
	public function edit(){
		
		return view('admin.user.edit');
	}
	
	
	//后台用户管理添加页面 GET
	public function create(){
		
		return view('admin.user.create');
	}
	
	
	
	//添加操作  POST
	public function store(Request $request){
     //dd($request->all());
        //规则
        $rules=[
            //'title' => 'required|unique:posts|max:255',
            'name'=>'required|unique:user|between:6,12',
            'pass'=>'required|between:6,12|same:repass'


        ];
        //所有数据
        $input=$request->all();

        //修改提示信息
        $message=array(
            "name.required"=>"请输入用户名",
            "name.unique"=>"用户名已存在",
            "name.between"=>"长度不满足",
            "pass.required"=>"请输入密码",
            "pass.between"=>"密码长度不满足",
            "pass.same"=>"两次不一致",
        );
        //设置表单验证的参数
        //参数一 需要验证的数据
        //参数二 验证规则
        //参数三 验证信息
        $validator = Validator::make($input,$rules,$message);

        //进行验证
        if($validator->passes()){
            //处理数据
            unset($_POST['_token']);
            unset($_POST['repass']);
            $_POST['pass']=Crypt::encrypt($_POST['pass']);
            $_POST['time']=time();
            //dd($_POST);
            if(DB::table("user")->insert($_POST)){
                return redirect("admin/user");
            }else{
                return back();
            }

        }else{
            //将输入存储到一次性 Session 然后重定向
            return back()->withInput()->withErrors($validator);
        }

/*
        //接受数据
        $name = $request->input('name');
        $pass = $request->input('pass');
        $repass = $request->input('repass');
        $statu = $request->input('statu');

        //将输入数据闪存至 Session
        $request->flash();
*/
        /*
        //判断用户名是否存在
        if($name){
            //判断用户名长度
            if(strlen($name)>=6 && strlen($name)<=12){
                //密码长度
                if(strlen($pass)>=6 && strlen($pass)<=12){
                    //判断两次密码是否一致
                    if($pass==$repass){
                        //判断用户名是否重名
                        $data=DB::table("user")->where("name",$name)->first();
                        if($data){
                            return back()->with('status', '用户名已存在');
                        }else{
                            //数据格式化
                            $arr=array(
                                "name"=>$name,
                                "pass"=>Crypt::encrypt("$pass"),
                                "statu"=>$statu,
                                "time"=>time()
                            );
                            if(DB::table('user')->insert($arr)){
                                return redirect("admin/user");
                            }else{
                                return back()->with('status', '增加数据库失败');
                            }

                        }
                    }else{
                        return back()->with('status', '两次密码不一致');
                    }

                }else{
                    return back()->with('status', '密码长度不够');
                }

            }else{
                return back()->with('status', '用户名长度不够');
            }

        }else{
            return back()->with('status', '用户名不存在');
        } */

	}
	
	//修改操作 PUT
	public function update(){
		
	}
	
	//删除操作  DELETE
	public function destory(){
		
	}
	
	
}
?>